package com.example.demo.controller;

import com.example.demo.core.Result;
import com.example.demo.entity.Online;
import com.example.demo.pojo.request.CreateOrUpdateOnlineRequest;
import com.example.demo.service.OnlineService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 网签配置控制器
 *
 * @author 叮当猫的百宝箱
 * @since 1.0
 */

@Tag(name = "网签配置管理", description = "网签配置管理")
@RestController
@RequestMapping("/online")
@RequiredArgsConstructor
public class OnlineController {

    private final OnlineService onlineService;

    /**
     * 添加网签配置
     *
     * @param createOnline 待添加的网签配置信息
     * @return API全局响应对象
     */
    @Operation(summary = "添加网签配置")
    @PostMapping("/create")
    @PreAuthorize("hasAuthority('/online/create')")
    public Result<Void> createOnline(@RequestBody @Validated CreateOrUpdateOnlineRequest createOnline) {
        Online online = new Online();
        BeanUtils.copyProperties(createOnline, online);
        onlineService.createOnline(online);
        return Result.ok();
    }

    /**
     * 删除网签配置
     *
     * @param onlineId 网签配置ID
     * @return API全局响应对象
     */
    @Operation(summary = "删除网签配置")
    @PostMapping("/{onlineId}/delete")
    @PreAuthorize("hasAuthority('/online/delete')")
    public Result<Void> deleteOnlineById(@PathVariable("onlineId") Long onlineId) {
        onlineService.deleteOnlineById(onlineId);
        return Result.ok();
    }

    /**
     * 更新网签配置信息
     *
     * @param onlineId     网签配置ID
     * @param updateOnline 待更新的网签配置信息
     * @return API全局响应对象
     */
    @Operation(summary = "更新网签配置")
    @PostMapping("/{onlineId}/update")
    @PreAuthorize("hasAuthority('/online/update')")
    public Result<Void> updateOnlineById(@PathVariable("onlineId") Long onlineId,
                                         @RequestBody @Validated CreateOrUpdateOnlineRequest updateOnline) {
        Online online = new Online();
        BeanUtils.copyProperties(updateOnline, online);
        onlineService.updateOnlineById(onlineId, online);
        return Result.ok();
    }

    /**
     * 获取网签配置列表
     *
     * @return API全局响应对象
     */
    @Operation(summary = "获取网签配置列表")
    @GetMapping
    @PreAuthorize("hasAuthority('/online/show')")
    public Result<List<Online>> getOnlineList() {
        List<Online> onlineList = onlineService.getAllOnline();
        return Result.ok(onlineList);
    }


}
